package com.xiaobaibai.controller;

import com.qiniu.util.Auth;
import com.xiaobaibai.common.QiniuCommon;
import com.xiaobaibai.response.ResponseFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@Api(tags = "七牛云token接口")
@RequestMapping("/qiniu")
@CrossOrigin
@Slf4j
public class QiniuController implements ApplicationRunner {

    private Auth auth = null;
    @Override
    public void run(ApplicationArguments args) {
        auth = Auth.create(QiniuCommon.accessKey, QiniuCommon.secretKey);
    }
    @GetMapping("/getToken")
    @ApiOperation("获取七牛图片上传token")
    public ResponseFormat uploadPic(){

        String qnToken=auth.uploadToken(QiniuCommon.bucket);
        String fileName=DateFormatUtils.format(new Date(), "yyyy/MM/dd/");
        fileName+= UUID.randomUUID().toString().substring(16);//从第十六位开始截取,(36-16)总共截取20个字符
        Map<String,String> map=new HashMap<>();
        map.put("qiniu_url","http://image.xiaobaibai.ink/"+fileName);
        map.put("qiniu_token",qnToken);
        map.put("qiniu_key",fileName);
        log.info("准备上传图片，文件名称:"+fileName);
        return ResponseFormat.ok("获取七牛上传token",map);
    }

}
